<!DOCTYPE html>
<html lang="en-us">
  <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    
<meta charset="UTF-8">
<title>Date Range Aggregation | Elasticsearch Guide [7.7] | Elastic</title>
<link rel="home" href="index.html" title="Elasticsearch Guide [7.7]">
<link rel="up" href="search-aggregations-bucket.html" title="Bucket Aggregations">
<link rel="prev" href="search-aggregations-bucket-datehistogram-aggregation.html" title="Date histogram aggregation">
<link rel="next" href="search-aggregations-bucket-diversified-sampler-aggregation.html" title="Diversified Sampler Aggregation">
<meta name="DC.type" content="Learn/Docs/Elasticsearch/Reference/7.7">
<meta name="DC.subject" content="Elasticsearch">
<meta name="DC.identifier" content="7.7">
<meta name="robots" content="noindex,nofollow">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://cdn.optimizely.com/js/18132920325.js"></script>
    <link rel="apple-touch-icon" sizes="57x57" href="/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-icon-180x180.png">
    <link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
    <link rel="icon" type="image/png" href="/android-chrome-192x192.png" sizes="192x192">
    <link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96">
    <link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
    <link rel="manifest" href="/manifest.json">
    <meta name="apple-mobile-web-app-title" content="Elastic">
    <meta name="application-name" content="Elastic">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="msapplication-TileImage" content="/mstile-144x144.png">
    <meta name="theme-color" content="#ffffff">
    <meta name="naver-site-verification" content="936882c1853b701b3cef3721758d80535413dbfd">
    <meta name="yandex-verification" content="d8a47e95d0972434">
    <meta name="localized" content="true">
    <meta name="st:robots" content="follow,index">
    <meta property="og:image" content="https://www.elastic.co/static/images/elastic-logo-200.png">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <link rel="apple-touch-icon-precomposed" sizes="64x64" href="/favicon_64x64_16bit.png">
    <link rel="apple-touch-icon-precomposed" sizes="32x32" href="/favicon_32x32.png">
    <link rel="apple-touch-icon-precomposed" sizes="16x16" href="/favicon_16x16.png">
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="/guide/static/styles.css">
  </head>

  <!--© 2015-2021 Elasticsearch B.V. Copying, publishing and/or distributing without written permission is strictly prohibited.-->

  <body>
    <!-- Google Tag Manager -->
    <script>dataLayer = [];</script><noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-58RLH5" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-58RLH5');</script>
    <!-- End Google Tag Manager -->

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-12395217-16"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'UA-12395217-16');
    </script>

    <!--BEGIN QUALTRICS WEBSITE FEEDBACK SNIPPET-->
    <script type="text/javascript">
      (function(){var g=function(e,h,f,g){
      this.get=function(a){for(var a=a+"=",c=document.cookie.split(";"),b=0,e=c.length;b<e;b++){for(var d=c[b];" "==d.charAt(0);)d=d.substring(1,d.length);if(0==d.indexOf(a))return d.substring(a.length,d.length)}return null};
      this.set=function(a,c){var b="",b=new Date;b.setTime(b.getTime()+6048E5);b="; expires="+b.toGMTString();document.cookie=a+"="+c+b+"; path=/; "};
      this.check=function(){var a=this.get(f);if(a)a=a.split(":");else if(100!=e)"v"==h&&(e=Math.random()>=e/100?0:100),a=[h,e,0],this.set(f,a.join(":"));else return!0;var c=a[1];if(100==c)return!0;switch(a[0]){case "v":return!1;case "r":return c=a[2]%Math.floor(100/c),a[2]++,this.set(f,a.join(":")),!c}return!0};
      this.go=function(){if(this.check()){var a=document.createElement("script");a.type="text/javascript";a.src=g;document.body&&document.body.appendChild(a)}};
      this.start=function(){var a=this;window.addEventListener?window.addEventListener("load",function(){a.go()},!1):window.attachEvent&&window.attachEvent("onload",function(){a.go()})}};
      try{(new g(100,"r","QSI_S_ZN_emkP0oSe9Qrn7kF","https://znemkp0ose9qrn7kf-elastic.siteintercept.qualtrics.com/WRSiteInterceptEngine/?Q_ZID=ZN_emkP0oSe9Qrn7kF")).start()}catch(i){}})();
    </script><div id="ZN_emkP0oSe9Qrn7kF"><!--DO NOT REMOVE-CONTENTS PLACED HERE--></div>
    <!--END WEBSITE FEEDBACK SNIPPET-->

    <div id="elastic-nav" style="display:none;"></div>
    <script src="https://www.elastic.co/elastic-nav.js"></script>

    <!-- Subnav -->
    <div>
      <div>
        <div class="tertiary-nav d-none d-md-block">
          <div class="container">
            <div class="p-t-b-15 d-flex justify-content-between nav-container">
              <div class="breadcrum-wrapper"><span><a href="/guide/" style="font-size: 14px; font-weight: 600; color: #000;">Docs</a></span></div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="main-container">
      <section id="content">
        <div class="content-wrapper">

          <section id="guide" lang="en">
            <div class="container">
              <div class="row">
                <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                  <!-- start body -->
                  <div class="page_header">
<strong>IMPORTANT</strong>: No additional bug fixes or documentation updates
will be released for this version. For the latest information, see the
<a href="../current/index.html">current release documentation</a>.
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">Aggregations</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-bucket.html">Bucket Aggregations</a></span>
»
<span class="breadcrumb-node">Date Range Aggregation</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-bucket-datehistogram-aggregation.html">« Date histogram aggregation</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-diversified-sampler-aggregation.html">Diversified Sampler Aggregation »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-bucket-daterange-aggregation"></a>Date Range Aggregation<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/daterange-aggregation.asciidoc">edit</a>
</h2>
</div></div></div>
<p>A range aggregation that is dedicated for date values. The main difference
between this aggregation and the normal
<a class="xref" href="search-aggregations-bucket-range-aggregation.html" title="Range Aggregation">range</a>
aggregation is that the <code class="literal">from</code> and <code class="literal">to</code> values can be expressed in
<a class="xref" href="common-options.html#date-math" title="Date Math">Date Math</a> expressions, and it is also possible to specify a date
format by which the <code class="literal">from</code> and <code class="literal">to</code> response fields will be returned.
Note that this aggregation includes the <code class="literal">from</code> value and excludes the <code class="literal">to</code> value
for each range.</p>
<p>Example:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
    "aggs": {
        "range": {
            "date_range": {
                "field": "date",
                "format": "MM-yyyy",
                "ranges": [
                    { "to": "now-10M/M" }, <a id="CO223-1"></a><i class="conum" data-value="1"></i>
                    { "from": "now-10M/M" } <a id="CO223-2"></a><i class="conum" data-value="2"></i>
                ]
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/452.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO223-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>&lt; now minus 10 months, rounded down to the start of the month.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO223-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>&gt;= now minus 10 months, rounded down to the start of the month.</p>
</td>
</tr>
</table>
</div>
<p>In the example above, we created two range buckets, the first will "bucket" all
documents dated prior to 10 months ago and the second will "bucket" all
documents dated since 10 months ago</p>
<p>Response:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "range": {
            "buckets": [
                {
                    "to": 1.4436576E12,
                    "to_as_string": "10-2015",
                    "doc_count": 7,
                    "key": "*-10-2015"
                },
                {
                    "from": 1.4436576E12,
                    "from_as_string": "10-2015",
                    "doc_count": 0,
                    "key": "10-2015-*"
                }
            ]
        }
    }
}</pre>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_missing_values_2"></a>Missing Values<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/daterange-aggregation.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The <code class="literal">missing</code> parameter defines how documents that are missing a value should
be treated. By default they will be ignored but it is also possible to treat
them as if they had a value. This is done by adding a set of fieldname :
value mappings to specify default values per field.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
   "aggs": {
       "range": {
           "date_range": {
               "field": "date",
               "missing": "1976/11/30",
               "ranges": [
                  {
                    "key": "Older",
                    "to": "2016/02/01"
                  }, <a id="CO224-1"></a><i class="conum" data-value="1"></i>
                  {
                    "key": "Newer",
                    "from": "2016/02/01",
                    "to" : "now/d"
                  }
              ]
          }
      }
   }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/453.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO224-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Documents without a value in the <code class="literal">date</code> field will be added to the "Older"
bucket, as if they had a date value of "1976-11-30".</p>
</td>
</tr>
</table>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="date-format-pattern"></a>Date Format/Pattern<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/daterange-aggregation.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>this information was copied from
<a href="https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html" class="ulink" target="_top">DateTimeFormatter</a></p>
</div>
</div>
<p>All ASCII letters are reserved as format pattern letters, which are defined
as follows:</p>
<div class="informaltable">
<table border="1" cellpadding="4px">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
<col class="col_4">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">Symbol</th>
<th align="left" valign="top">Meaning</th>
<th align="left" valign="top">Presentation</th>
<th align="left" valign="top">Examples</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p>G</p></td>
<td align="left" valign="top"><p>era</p></td>
<td align="left" valign="top"><p>text</p></td>
<td align="left" valign="top"><p>AD; Anno Domini; A</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>u</p></td>
<td align="left" valign="top"><p>year</p></td>
<td align="left" valign="top"><p>year</p></td>
<td align="left" valign="top"><p>2004; 04</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>y</p></td>
<td align="left" valign="top"><p>year-of-era</p></td>
<td align="left" valign="top"><p>year</p></td>
<td align="left" valign="top"><p>2004; 04</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>D</p></td>
<td align="left" valign="top"><p>day-of-year</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>189</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>M/L</p></td>
<td align="left" valign="top"><p>month-of-year</p></td>
<td align="left" valign="top"><p>number/text</p></td>
<td align="left" valign="top"><p>7; 07; Jul; July; J</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>d</p></td>
<td align="left" valign="top"><p>day-of-month</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>10</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>Q/q</p></td>
<td align="left" valign="top"><p>quarter-of-year</p></td>
<td align="left" valign="top"><p>number/text</p></td>
<td align="left" valign="top"><p>3; 03; Q3; 3rd quarter</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>Y</p></td>
<td align="left" valign="top"><p>week-based-year</p></td>
<td align="left" valign="top"><p>year</p></td>
<td align="left" valign="top"><p>1996; 96</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>w</p></td>
<td align="left" valign="top"><p>week-of-week-based-year</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>27</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>W</p></td>
<td align="left" valign="top"><p>week-of-month</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>4</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>E</p></td>
<td align="left" valign="top"><p>day-of-week</p></td>
<td align="left" valign="top"><p>text</p></td>
<td align="left" valign="top"><p>Tue; Tuesday; T</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>e/c</p></td>
<td align="left" valign="top"><p>localized day-of-week</p></td>
<td align="left" valign="top"><p>number/text</p></td>
<td align="left" valign="top"><p>2; 02; Tue; Tuesday; T</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>F</p></td>
<td align="left" valign="top"><p>week-of-month</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>3</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>a</p></td>
<td align="left" valign="top"><p>am-pm-of-day</p></td>
<td align="left" valign="top"><p>text</p></td>
<td align="left" valign="top"><p>PM</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>h</p></td>
<td align="left" valign="top"><p>clock-hour-of-am-pm (1-12)</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>12</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>K</p></td>
<td align="left" valign="top"><p>hour-of-am-pm (0-11)</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>0</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>k</p></td>
<td align="left" valign="top"><p>clock-hour-of-am-pm (1-24)</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>0</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>H</p></td>
<td align="left" valign="top"><p>hour-of-day (0-23)</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>0</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>m</p></td>
<td align="left" valign="top"><p>minute-of-hour</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>30</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>s</p></td>
<td align="left" valign="top"><p>second-of-minute</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>55</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>S</p></td>
<td align="left" valign="top"><p>fraction-of-second</p></td>
<td align="left" valign="top"><p>fraction</p></td>
<td align="left" valign="top"><p>978</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>A</p></td>
<td align="left" valign="top"><p>milli-of-day</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>1234</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>n</p></td>
<td align="left" valign="top"><p>nano-of-second</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>987654321</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>N</p></td>
<td align="left" valign="top"><p>nano-of-day</p></td>
<td align="left" valign="top"><p>number</p></td>
<td align="left" valign="top"><p>1234000000</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>V</p></td>
<td align="left" valign="top"><p>time-zone ID</p></td>
<td align="left" valign="top"><p>zone-id</p></td>
<td align="left" valign="top"><p>America/Los_Angeles; Z; -08:30</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>z</p></td>
<td align="left" valign="top"><p>time-zone name</p></td>
<td align="left" valign="top"><p>zone-name</p></td>
<td align="left" valign="top"><p>Pacific Standard Time; PST</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>O</p></td>
<td align="left" valign="top"><p>localized zone-offset</p></td>
<td align="left" valign="top"><p>offset-O</p></td>
<td align="left" valign="top"><p>GMT+8; GMT+08:00; UTC-08:00;</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>X</p></td>
<td align="left" valign="top"><p>zone-offset <em>Z</em> for zero</p></td>
<td align="left" valign="top"><p>offset-X</p></td>
<td align="left" valign="top"><p>Z; -08; -0830; -08:30; -083015; -08:30:15;</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>x</p></td>
<td align="left" valign="top"><p>zone-offset</p></td>
<td align="left" valign="top"><p>offset-x</p></td>
<td align="left" valign="top"><p>+0000; -08; -0830; -08:30; -083015; -08:30:15;</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>Z</p></td>
<td align="left" valign="top"><p>zone-offset</p></td>
<td align="left" valign="top"><p>offset-Z</p></td>
<td align="left" valign="top"><p>+0000; -0800; -08:00;</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>p</p></td>
<td align="left" valign="top"><p>pad next</p></td>
<td align="left" valign="top"><p>pad modifier</p></td>
<td align="left" valign="top"><p>1</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>'</p></td>
<td align="left" valign="top"><p>escape for text</p></td>
<td align="left" valign="top"><p>delimiter</p></td>
<td align="left" valign="top"><p>''</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>single quote</p></td>
<td align="left" valign="top"><p>literal</p></td>
<td align="left" valign="top"><p>'</p></td>
<td align="left" valign="top"><p>[</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>optional section start</p></td>
<td align="left" valign="top"><p>]</p></td>
<td align="left" valign="top"><p>optional section end</p></td>
<td align="left" valign="top"><p>#</p></td>
</tr>
<tr>
<td align="left" valign="top"><p>reserved for future use</p></td>
<td align="left" valign="top"><p>{</p></td>
<td align="left" valign="top"><p>reserved for future use</p></td>
<td align="left" valign="top"><p>}</p></td>
</tr>
</tbody>
</table>
</div>
<p>The count of pattern letters determines the format.</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
Text
</span>
</dt>
<dd>
The text style is determined based on the number of pattern letters
used. Less than 4 pattern letters will use the short form. Exactly 4
pattern letters will use the full form. Exactly 5 pattern letters will use
the narrow form. Pattern letters <code class="literal">L</code>, <code class="literal">c</code>, and <code class="literal">q</code> specify the stand-alone
form of the text styles.
</dd>
<dt>
<span class="term">
Number
</span>
</dt>
<dd>
If the count of letters is one, then the value is output using
the minimum number of digits and without padding. Otherwise, the count of
digits is used as the width of the output field, with the value
zero-padded as necessary. The following pattern letters have constraints
on the count of letters. Only one letter of <code class="literal">c</code> and <code class="literal">F</code> can be specified.
Up to two letters of <code class="literal">d</code>, <code class="literal">H</code>, <code class="literal">h</code>, <code class="literal">K</code>, <code class="literal">k</code>, <code class="literal">m</code>, and <code class="literal">s</code> can be
specified. Up to three letters of <code class="literal">D</code> can be specified.
</dd>
<dt>
<span class="term">
Number/Text
</span>
</dt>
<dd>
If the count of pattern letters is 3 or greater, use the
Text rules above. Otherwise use the Number rules above.
</dd>
<dt>
<span class="term">
Fraction
</span>
</dt>
<dd>
Outputs the nano-of-second field as a fraction-of-second. The
nano-of-second value has nine digits, thus the count of pattern letters is
from 1 to 9. If it is less than 9, then the nano-of-second value is
truncated, with only the most significant digits being output.
</dd>
<dt>
<span class="term">
Year
</span>
</dt>
<dd>
The count of letters determines the minimum field width below which
padding is used. If the count of letters is two, then a reduced two digit
form is used. For printing, this outputs the rightmost two digits. For
parsing, this will parse using the base value of 2000, resulting in a year
within the range 2000 to 2099 inclusive. If the count of letters is less
than four (but not two), then the sign is only output for negative years
as per <code class="literal">SignStyle.NORMAL</code>. Otherwise, the sign is output if the pad width is
exceeded, as per <code class="literal">SignStyle.EXCEEDS_PAD</code>.
</dd>
<dt>
<span class="term">
ZoneId
</span>
</dt>
<dd>
This outputs the time-zone ID, such as <code class="literal">Europe/Paris</code>. If the
count of letters is two, then the time-zone ID is output. Any other count
of letters throws <code class="literal">IllegalArgumentException</code>.
</dd>
<dt>
<span class="term">
Zone names
</span>
</dt>
<dd>
This outputs the display name of the time-zone ID. If the
count of letters is one, two or three, then the short name is output. If
the count of letters is four, then the full name is output. Five or more
letters throws <code class="literal">IllegalArgumentException</code>.
</dd>
<dt>
<span class="term">
Offset X and x
</span>
</dt>
<dd>
This formats the offset based on the number of pattern
letters. One letter outputs just the hour, such as <code class="literal">+01</code>, unless the
minute is non-zero in which case the minute is also output, such as
<code class="literal">+0130</code>. Two letters outputs the hour and minute, without a colon, such as
<code class="literal">+0130</code>. Three letters outputs the hour and minute, with a colon, such as
<code class="literal">+01:30</code>. Four letters outputs the hour and minute and optional second,
without a colon, such as <code class="literal">+013015</code>. Five letters outputs the hour and
minute and optional second, with a colon, such as <code class="literal">+01:30:15</code>. Six or
more letters throws <code class="literal">IllegalArgumentException</code>. Pattern letter <code class="literal">X</code> (upper
case) will output <code class="literal">Z</code> when the offset to be output would be zero,
whereas pattern letter <code class="literal">x</code> (lower case) will output <code class="literal">+00</code>, <code class="literal">+0000</code>, or
<code class="literal">+00:00</code>.
</dd>
<dt>
<span class="term">
Offset O
</span>
</dt>
<dd>
This formats the localized offset based on the number of
pattern letters. One letter outputs the short form of the localized
offset, which is localized offset text, such as <code class="literal">GMT</code>, with hour without
leading zero, optional 2-digit minute and second if non-zero, and colon,
for example <code class="literal">GMT+8</code>. Four letters outputs the full form, which is
localized offset text, such as <code class="literal">GMT, with 2-digit hour and minute
field, optional second field if non-zero, and colon, for example
`GMT+08:00</code>. Any other count of letters throws
<code class="literal">IllegalArgumentException</code>.
</dd>
<dt>
<span class="term">
Offset Z
</span>
</dt>
<dd>
This formats the offset based on the number of pattern letters.
One, two or three letters outputs the hour and minute, without a colon,
such as <code class="literal">+0130</code>. The output will be <code class="literal">+0000</code> when the offset is zero.
Four letters outputs the full form of localized offset, equivalent to
four letters of Offset-O. The output will be the corresponding localized
offset text if the offset is zero. Five letters outputs the hour,
minute, with optional second if non-zero, with colon. It outputs <code class="literal">Z</code> if
the offset is zero. Six or more letters throws IllegalArgumentException.
</dd>
<dt>
<span class="term">
Optional section
</span>
</dt>
<dd>
The optional section markers work exactly like calling
<code class="literal">DateTimeFormatterBuilder.optionalStart()</code> and
<code class="literal">DateTimeFormatterBuilder.optionalEnd()</code>.
</dd>
<dt>
<span class="term">
Pad modifier
</span>
</dt>
<dd>
Modifies the pattern that immediately follows to be padded
with spaces. The pad width is determined by the number of pattern letters.
This is the same as calling <code class="literal">DateTimeFormatterBuilder.padNext(int)</code>.
</dd>
</dl>
</div>
<p>For example, <code class="literal">ppH</code> outputs the hour-of-day padded on the left with spaces to a width of 2.</p>
<p>Any unrecognized letter is an error. Any non-letter character, other than
<code class="literal">[</code>, <code class="literal">]</code>, <code class="literal">{</code>, <code class="literal">}</code>, <code class="literal">#</code> and the single quote will be output directly.
Despite this, it is recommended to use single quotes around all characters
that you want to output directly to ensure that future changes do not
break your application.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="time-zones"></a>Time zone in date range aggregations<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/daterange-aggregation.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Dates can be converted from another time zone to UTC by specifying the
<code class="literal">time_zone</code> parameter.</p>
<p>Time zones may either be specified as an ISO 8601 UTC offset (e.g. +01:00 or
-08:00) or as one of the time zone ids from the TZ database.</p>
<p>The <code class="literal">time_zone</code> parameter is also applied to rounding in date math expressions.
As an example, to round to the beginning of the day in the CET time zone, you
can do the following:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
   "aggs": {
       "range": {
           "date_range": {
               "field": "date",
               "time_zone": "CET",
               "ranges": [
                  { "to": "2016/02/01" }, <a id="CO225-1"></a><i class="conum" data-value="1"></i>
                  { "from": "2016/02/01", "to" : "now/d" }, <a id="CO225-2"></a><i class="conum" data-value="2"></i>
                  { "from": "now/d" }
              ]
          }
      }
   }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/454.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO225-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>This date will be converted to <code class="literal">2016-02-01T00:00:00.000+01:00</code>.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO225-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">now/d</code> will be rounded to the beginning of the day in the CET time zone.</p>
</td>
</tr>
</table>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_keyed_response_4"></a>Keyed Response<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/aggregations/bucket/daterange-aggregation.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Setting the <code class="literal">keyed</code> flag to <code class="literal">true</code> will associate a unique string key with each
bucket and return the ranges as a hash rather than an array:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
    "aggs": {
        "range": {
            "date_range": {
                "field": "date",
                "format": "MM-yyy",
                "ranges": [
                    { "to": "now-10M/M" },
                    { "from": "now-10M/M" }
                ],
                "keyed": true
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/455.console"></div>
<p>Response:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "range": {
            "buckets": {
                "*-10-2015": {
                    "to": 1.4436576E12,
                    "to_as_string": "10-2015",
                    "doc_count": 7
                },
                "10-2015-*": {
                    "from": 1.4436576E12,
                    "from_as_string": "10-2015",
                    "doc_count": 0
                }
            }
        }
    }
}</pre>
</div>
<p>It is also possible to customize the key for each range:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search?size=0
{
    "aggs": {
        "range": {
            "date_range": {
                "field": "date",
                "format": "MM-yyy",
                "ranges": [
                    { "from": "01-2015",  "to": "03-2015", "key": "quarter_01" },
                    { "from": "03-2015", "to": "06-2015", "key": "quarter_02" }
                ],
                "keyed": true
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/456.console"></div>
<p>Response:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "range": {
            "buckets": {
                "quarter_01": {
                    "from": 1.4200704E12,
                    "from_as_string": "01-2015",
                    "to": 1.425168E12,
                    "to_as_string": "03-2015",
                    "doc_count": 5
                },
                "quarter_02": {
                    "from": 1.425168E12,
                    "from_as_string": "03-2015",
                    "to": 1.4331168E12,
                    "to_as_string": "06-2015",
                    "doc_count": 2
                }
            }
        }
    }
}</pre>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-bucket-datehistogram-aggregation.html">« Date histogram aggregation</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-diversified-sampler-aggregation.html">Diversified Sampler Aggregation »</a>
</span>
</div>
</div>

                  <!-- end body -->
                </div>
                <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                  <div id="rtpcontainer" style="display: block;">
                    <div class="mktg-promo">
                      <h3>Most Popular</h3>
                      <ul class="icons">
                        <li class="icon-elasticsearch-white"><a href="https://www.elastic.co/webinars/getting-started-elasticsearch?baymax=default&amp;elektra=docs&amp;storm=top-video">Get Started with Elasticsearch: Video</a></li>
                        <li class="icon-kibana-white"><a href="https://www.elastic.co/webinars/getting-started-kibana?baymax=default&amp;elektra=docs&amp;storm=top-video">Intro to Kibana: Video</a></li>
                        <li class="icon-logstash-white"><a href="https://www.elastic.co/webinars/introduction-elk-stack?baymax=default&amp;elektra=docs&amp;storm=top-video">ELK for Logs &amp; Metrics: Video</a></li>
                      </ul>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </section>

        </div>


<div id="elastic-footer"></div>
<script src="https://www.elastic.co/elastic-footer.js"></script>
<!-- Footer Section end-->

      </section>
    </div>

<script src="/guide/static/jquery.js"></script>
<script type="text/javascript" src="/guide/static/docs.js"></script>
<script type="text/javascript">
  window.initial_state = {}</script>
  </body>
</html>
